package com.example.kehoobackend.controller;

import com.example.kehoobackend.common.CommonResponse;
import com.example.kehoobackend.domain.Comment;
import com.example.kehoobackend.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Validated
@CrossOrigin
@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 获取最新评论
     *
     * @param from_id : 评论的对象的id
     * @param to_id : 被评论的对象的id
     * @param type  : 被评论的对象的类型
     * @return CommonResponse<Map<Comment,List<Comment>>> : 最新评论集
     */
    @GetMapping("/comment/latest")
    public CommonResponse<List<Comment>> getLatestComments(
            @RequestParam("from_id") Integer from_id,
            @RequestParam("to_id") Integer to_id,
            @RequestParam("type") Integer type) {
        return CommonResponse.createForSuccess("最新评论集", commentService.getCommentResponses(commentService.getNewestComments(from_id,to_id, type)));
    }

    /**
     * 获取默认评论
     *
     * @param from_id : 评论的对象的id
     * @param to_id : 被评论的对象的id
     * @param type  : 被评论的对象的类型
     * @return CommonResponse<Map<Comment,List<Comment>>> : 默认评论集
     */
    @GetMapping("/comment/default")
    public CommonResponse<List<Comment>> getDefaultComments(
            @RequestParam("from_id") Integer from_id,
            @RequestParam("to_id") Integer to_id,
            @RequestParam("type") Integer type) {
        return CommonResponse.createForSuccess("默认评论集", commentService.getCommentResponses(commentService.getDefaultComments(from_id,to_id, type)));
    }

    /**
     * 查询用户是否已点赞某评论
     *
     * @param comment_id : 评论的id
     * @param user_id : 用户的id
     * @return CommonResponse<Boolean> : 是否已点赞
     */
    @GetMapping("/comment/like")
    public CommonResponse<Boolean> isLiked(
            @RequestParam("comment_id") Integer comment_id,
            @RequestParam("user_id") Integer user_id) {
        return commentService.isLike(comment_id, user_id)
                ? CommonResponse.createForSuccess("已点赞", true)
                : CommonResponse.createForSuccess("未点赞", false);
    }

    /**
     * 根据评论点赞数降序获取评论
     *
     * @param from_id : 评论的对象的id
     * @param to_id : 被评论的对象的id
     * @param type  : 被评论的对象的类型
     * @return CommonResponse<Map<Comment,List<Comment>> > : 评论集
     */
    @GetMapping("/comment/like/desc")
    public CommonResponse<List<Comment>> getCommentsByLikeDesc(
            @RequestParam("from_id") Integer from_id,
            @RequestParam("to_id") Integer to_id,
            @RequestParam("type") Integer type) {
        return CommonResponse.createForSuccess("评论集", commentService.getCommentResponses(commentService.getCommentsByLikeDesc(from_id,to_id, type)));
    }




    /**
     * 发送评论
     *
     * @param comment : 评论
     * @return CommonResponse<Comment> : 是否发送成功
     */
    @PostMapping("/comment")
    public CommonResponse<Comment> commentOn(
            @RequestBody Comment comment) {
        return CommonResponse.createForSuccess("新增评论",commentService.commentOn(comment));
    }





    /**
     * 点赞评论
     *
     * @param comment_id : 评论的id
     * @param user_id    : 用户的id
     * @return : 是否点赞成功
     */
    @PatchMapping("/comment/like")
    public CommonResponse<Boolean> like(
            @RequestParam("comment_id") Integer comment_id,
            @RequestParam("user_id") Integer user_id) {
        return commentService.like(comment_id, user_id)
                ? CommonResponse.createForSuccess("点赞成功", true)
                : CommonResponse.createForSuccess("点赞失败", false);
    }

    /**
     * 取消点赞
     *
     * @param comment_id : 评论的id
     * @param user_id    : 用户的id
     * @return : 是否取消点赞成功
     */
    @PatchMapping("/comment/like/cancel")
    public CommonResponse<Boolean> cancelLike(
            @RequestParam("comment_id") Integer comment_id,
            @RequestParam("user_id") Integer user_id) {
        return commentService.cancelLike(comment_id, user_id)
                ? CommonResponse.createForSuccess("取消点赞成功", true)
                : CommonResponse.createForSuccess("取消点赞失败", false);
    }




    /**
     * 删除评论
     *
     * @param comment_id : 评论的id
     * @return : 是否删除成功
     */
    @DeleteMapping("/comment")
    public CommonResponse<Boolean> deleteComment(
            @RequestParam("comment_id") Integer comment_id) {
        return commentService.deleteComment(comment_id) ? CommonResponse.createForSuccess("删除评论成功", true) : CommonResponse.createForSuccess("删除评论失败", false);
    }
}


